
You may recall from my last post, that I boasted about how simple it would be to implement some sort of fire hazard system, where random buildings would burn down, if you didn't have enough fire departments to protect your town. I believe my exact words were:
face a random blaze every now and again, requiring you to rebuild buildings as they burn down. This doesn't seem hard to implement, so I may give it a try and see how I like it.
A bold statement for a wanna be programmer whose C++ skills are lacking, and who knows little to no JavaScript! I did not realize that I would burn five or six hours on this tiny little bit of code alone! What I thought would be simple turned into a huge learning curve that despite eventually getting the results I wanted, didn't lead me to learning the proper way to do it.
Right off the cuff, I wrote a few simple lines of code in under 10 minutes, fired up the game with $ clickable desktop (a type of emulator, you might say, just running the click on your own desktop with a lot of environment variables and things set, as if it was on your phone). I built a little town, and sat back to watch it burn down. And it did. Actually, the code I wrote worked right off the bat, or so I thought.
What actually happened was that it did randomly pick a tile and set it ablaze, leaving the hot, smoldering ashes tile behind it. I thought I'd mastered that, and would have committed my code right then and there, but I thought I should diligently test it a bit more, just to be sure it works properly, and I'm glad I did. Because it didn't. At all.
The first major failure was that what I wrote switched which tool you had selected to now be a "blaze" tool, causing everything the player clicked/tapped on to set on fire. E.g., a flaming version of Midas' touch. Well, that wasn't what I wanted to happen at all. So I fixed it. But now it only worked if you had a tool selected, and didn't work if you did not have a tool selected. So I fixed that. But then it caused "mapBlock" errors, messing up which block was which in the game. So I fixed that, only to have it appear to work, but when you clicked on the smoldered ash tile left behind, the game would lock up. And let me tell you, if you saved the game, while there was an ash tile, you can forget ever loading that saved game back up again!
Needless to say, it took me about six hours to fix my original code. So much changed that I actually started over several times during the process. But, praise God, I eventually got it. In the end, what became the final result actually worked out even better than I had originally planned, because now I could use the new code for other hazards!
Originally, I was just working on a little code for a fire hazard tile. After rewriting it several times, I realized that I could move the bulk of the code into it's own function which I could then call for other hazards, not just fires. Things like a volcano, or a flood. Maybe king kong, or some other disaster like a tornado! While it was my errors that took me out of my way, I'm glad they did, because now the code is much better for implementing other disasters and hazards.
Essentially, I took the "block" code, which is used to make a building when you click on a tool and then on a square of the map, and added a "hazzard" number.
Yes, I know that is misspelled. And yes, when I read it, I also think of Bo and Luke Duke and the rest of the gang. But it was actually convenient to misspell it for searching purposes, so I could jump right to the variable, ignoring any other use of the word hazard. Anyways... back on topic.
This "hazzard" number, when 0, would allow the original block code for point and click/tap to build whatever tool you had selected. However, if the number was above 0, it would be a special hazard. In this case, hazzard 1 is a plain old fire, known as a "blaze". It is also handy, because once I implement meteors, volcano, and other hazards, I can reuse "blaze" for the surrounding areas, since they would be on fire after those events as well.
Either way, I'm glad the code is now properly implemented and fixed, and that I can use it for other fun things!
Don't forget that you can check out the fire hazard commits on Gitlab. [1]
Linux - keep it simple.